﻿using System.Linq;
using Fee.Orders.AuthProjectWorkflow;
using Fee.Orders.DesignWorkflow;
using Fee.Orders.FormalWorkflow;
using Fee.Orders.PreWorkflow;

namespace JwtWebApi.Data
{
    public static class WorkflowDataSeed
    {
        /// <summary>
        ///     流程2
        /// </summary>
        /// <param name="dbContext"></param>
        public static void Settlememnt(ApplicationDbContext dbContext)
        {
            var entity =
                dbContext.BudgetOrderWorkProcess.FirstOrDefault(f =>
                    f.Name == BudgetOrder.DesignPlanCostWorkflowName);
            if (entity != null)
                return;
            var workflow = new
                BudgetOrderWorkProcess
            {
                Name = BudgetOrder.DesignPlanCostWorkflowName
            };
            var step1 = new BudgetOrderWorkTask
            {
                Name = BudgetOrder.DesignPlanCostStep1,
                Role = dbContext.Roles.FirstOrDefault(f => f.NormalizedName == "designer".ToUpper()),
                DefaultResolveComment = "初步预算已完成",
                CanReject = false,
                CanGiveUp = false,
                Ordered = 1,
                CanEdit = true
            };

            var step2 = new BudgetOrderWorkTask
            {
                Name = BudgetOrder.DesignPlanCostStep2,
                Role = dbContext.Roles.FirstOrDefault(f => f.NormalizedName == "company".ToUpper()),
                CanGiveUp = true,
                CanReject = true,
                CanEdit = false,
                Ordered = 2
            };

            var step3 = new BudgetOrderWorkTask
            {
                Name = BudgetOrder.DesignPlanCostPlanStep3,
                Role = dbContext.Roles.FirstOrDefault(f => f.NormalizedName == "constructionCenter".ToUpper()),
                CanReject = true,
                Ordered = 3
            };


            workflow.Add(step1);
            workflow.Add(step2);
            workflow.Add(step3);

            dbContext.Update(workflow);
            dbContext.SaveChanges();
        }

        /// <summary>
        ///     流程1
        /// </summary>
        /// <param name="dbContext"></param>
        public static void PreDelegateDeisgn(ApplicationDbContext dbContext)
        {
            var workProcess =
                dbContext.PreDelegateWorkProcesses.FirstOrDefault(f => f.Name == PreDelegateOrder.WorkflowName);
            if (workProcess != null)
                return;
            var step1Role = "delegateAdmin";
            var step2Role = "designer";
            workProcess = new PreDelegateWorkProcess { Name = PreDelegateOrder.WorkflowName };
            workProcess.Add(new PreDelegateWorkTask
            {
                CanEdit = false,
                CanReject = false,
                CanChangedDesigner = true,
                Name = PreDelegateOrder.EditStepWorkFlowItem,
                Role = dbContext.Roles.FirstOrDefault(f => f.Name == step1Role),
                Ordered = 0
            });
            workProcess.Add(new PreDelegateWorkTask
            {
                CanEdit = false,
                CanReject = false,
                CanChangedDesigner = false,
                Name = PreDelegateOrder.NotifyDesigner,
                Role = dbContext.Roles.FirstOrDefault(f => f.Name == step2Role),
                DefaultResolveComment = "已经收到",
                Ordered = 1
            });

            dbContext.Update(workProcess);
            dbContext.SaveChanges();
        }


        public static void DelegateOrderDesign(ApplicationDbContext dbContext)
        {
            var entity =
                dbContext.FormalOrderWorkProcesses.FirstOrDefault(f =>
                    f.Name == FormalOrder.FormalDelegateWorkflowName);
            if (entity != null)
                return;
            var workflow = new
                FormalOrderWorkProcess
            {
                Name = FormalOrder.FormalDelegateWorkflowName
            };
            var step1 = new FormalOrderWorkTask
            {
                Name = FormalOrder.FormalDelegateStep1,
                Role = dbContext.Roles.FirstOrDefault(f => f.NormalizedName == "delegateAdmin".ToUpper()),
                Ordered = 1,
                CanEdit = true
            };
            workflow.Add(step1);
            dbContext.Update(workflow);
            dbContext.SaveChanges();
        }


        public static void AuthProjectOrderItem(ApplicationDbContext dbContext)
        {
            var entity =
                dbContext.AuthProjectWorkProcesses.FirstOrDefault(f =>
                    f.Name == AuthProjectOrder.WorkProcessName);
            if (entity != null)
                return;

            var workflow = new
                AuthProjectWorkProcess()
            {
                Name = AuthProjectOrder.WorkProcessName
            };
            var step1 = new AuthProjectWorkTask()
            {
                Name = AuthProjectOrder.DesignUpload,
                Role = dbContext.Roles.FirstOrDefault(f => f.NormalizedName == "designer".ToUpper()),
                Ordered = 1,
                CanEdit = true
            };

            var step2 = new AuthProjectWorkTask()
            {
                Name = AuthProjectOrder.ProjectAuth,
                Role = dbContext.Roles.FirstOrDefault(f => f.NormalizedName == "projectOwner".ToUpper()),
                Ordered = 2,
                CanEdit = true,
                CanReject = true,
            };
            workflow.Add(step1);
            workflow.Add(step2);
            dbContext.Update(workflow);
            dbContext.SaveChanges();
        }

    }
}